package com.ruoyi.tester.executor.expression;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author SongJun
 * @description TODO:
 * @date 2021/5/31 17:10
 */
public class ExpressionContainer {
    private static Map<String, ExpressionAnalysis> handlerMap = new HashMap<>();

    synchronized public static void registerExpression(String name, ExpressionAnalysis handler){
        if(handlerMap.containsKey(name)){
            throw new RuntimeException("重复的表达式名:"+name);
        }
        handlerMap.put(name, handler);
    }

    public static ExpressionAnalysis getExpressionAnalysis(String name){
        ExpressionAnalysis handler = handlerMap.get(name);
        if(handler == null){
            throw new RuntimeException("无法解析表达式"+name+", 未找到对应的处理器");
        }
        return handler;
    }

    public static List<String> expressionsDescription(){
        List<String> ret = handlerMap.values().stream().map(x -> x.getDescription()).sorted().collect(Collectors.toList());
        return ret;
    }

    public static List<String> expressionsName(){
        List<String> ret = handlerMap.values().stream().map(x -> x.getExpressionName()).sorted().collect(Collectors.toList());
        return ret;
    }
}
